﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using FCModel;
using System.Reflection;
using FCBLL;
using MiscLib;
using System.Data;
using System.Text;

namespace FileConverter.QPM
{
    public partial class CSVQPMResonance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //If first time page is submitted and we have file in FileUpload control but not in session
            // Store the values to SEssion Object
            if (Session["FileUpload1"] == null && FileUpload_QPMHookUP.HasFile)
            {
                Session["FileUpload1"] = FileUpload_QPMHookUP;
                Label1.Text = FileUpload_QPMHookUP.FileName;
            }
            // Next time submit and Session has values but FileUpload is Blank
            // Return the values from session to FileUpload
            else if (Session["FileUpload1"] != null && (!FileUpload_QPMHookUP.HasFile))
            {
                FileUpload_QPMHookUP = (FileUpload)Session["FileUpload1"];
                Label1.Text = FileUpload_QPMHookUP.FileName;
            }
            // Now there could be another sictution when Session has File but user want to change the file
            // In this case we have to change the file in session object
            else if (FileUpload_QPMHookUP.HasFile)
            {
                Session["FileUpload1"] = FileUpload_QPMHookUP;
                Label1.Text = FileUpload_QPMHookUP.FileName;
            }
        }

        protected void ImportQPMCSV_Click(object sender, EventArgs e)
        {
            if (FileUpload_QPMHookUP.PostedFile.FileName == "" || FileUpload_QPMHookUP.PostedFile.InputStream.Length < 1)
            {
                tipsInfo.Text = "TestResult 没有指定文件.";
                return;
            }
            try
            {
                ImportQPMCSV.Enabled = false;
                string QPMHookUPFile = Guid.NewGuid().ToString() + Path.GetExtension(FileUpload_QPMHookUP.FileName);
                if (!QPMHookUPFile.EndsWith(".CSV", StringComparison.OrdinalIgnoreCase))
                {
                    tipsInfo.Text = "QPMHookUP 文件格式不正确";
                    return;
                }
                if (!Directory.Exists(Server.MapPath("~/CSVResonance")))
                {
                    Directory.CreateDirectory(Server.MapPath("~/CSVResonance/"));
                }
                FileUpload_QPMHookUP.PostedFile.SaveAs(Server.MapPath("~/CSVResonance/") + QPMHookUPFile);
                //= CommonBLL.CSVTODS(Server.MapPath("~/CSVResonance/") + QPMHookUPFile, 0, Encoding.GetEncoding("GB2312"));
                DataTable dt_Excel = HookUPQPMBLL.ReadQPMFile(Server.MapPath("~/CSVResonance/") + QPMHookUPFile);
                GridView_excel.DataSource = dt_Excel;
                GridView_excel.DataBind();

                string DFDRev = "1.4";
                string DocName = "HOOKUPCCP";
                string tableName = "rt_QPM2_Hookup";

                string filename1 = "098533326_545285111_PCBADFD_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");

                string logPath = Server.MapPath("~/CSVResonance/");


                if (Label1.Text == "")
                {
                    tipsInfo.Text = "没有指定文件.";
                }
                else
                {
                    string csvFile = Server.MapPath("~/CSVResonance/") + Label1.Text;
                    if (dt_Excel.Rows.Count < 1)
                    {
                        return;
                    }
                    string sql = string.Format("select * from " + tableName + "(nolock) where null = null");
                    List<string> list_val = HookUPQPMBLL.GetCol();

                    string static_info = "SOF" + Environment.NewLine + "# 54-528-5111" + Environment.NewLine + "# " + DocName + Environment.NewLine;
                    string file_head = string.Empty;

                    Dictionary<string, string> dc_txt = Get_Text();

                    string sb = HookUPQPMBLL.HookUPQPMToStr(dt_Excel, list_val, static_info, file_head, DFDRev, dc_txt);
                    TextBox_DFD.Text = sb;

                    string save_str = static_info + "\"FILE_NAME\",\"FILE_CREATION_DATE\",\"NUMBER_OF_RECORDS\"" + Environment.NewLine
                        + "\"" + new FileInfo(filename1).Name + "\",\"" + filename1.Split('_')[3].Split('.')[0] + "\","
                        + dt_Excel.Rows.Count + Environment.NewLine + "EOF";
                    string filename2 = filename1.Replace("PCBADFD", "PCBAMF");
                    TextBox__MF.Text = save_str;

                    Session["filename1"] = filename1;
                    Session["filename2"] = filename2;
                }
            }
            catch (Exception err)
            {
                tipsInfo.Text = err.Message;
            }
        }


        private Dictionary<string, string> Get_Text()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            dc.Add("MFG_SITE_DUNS", "54-528-5111");
            dc.Add("SUB_SUPPLIER_NAME", "KAIFA");
            dc.Add("DATA_SOURCE_TYPE", TextBox3.Text.Trim());
            dc.Add("PART_NUM", TextBox4.Text.Trim());
            dc.Add("PART_REV", TextBox5.Text.Trim());
            dc.Add("DRAWING_NUM", TextBox6.Text.Trim());
            dc.Add("DRAWING_REV", TextBox7.Text.Trim());
            dc.Add("GROUP_DATETIME", TextBox8.Text.Trim());
            dc.Add("SBR_NUM", TextBox9.Text.Trim());
            dc.Add("LOT_NUM", TextBox10.Text.Trim());
            dc.Add("RECORD_DATETIME", TextBox11.Text.Trim());
            dc.Add("REFERENCE_PART_NUM", TextBox12.Text.Trim());
            dc.Add("REFERENCE_PART_REV", TextBox13.Text.Trim());
            return dc;
        }

        protected void CreateQPMfile_1_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelExport.OutputTxt((string)Session["filename1"], TextBox_DFD.Text, HttpContext.Current.Response, Server);
                Session.Remove("filename1");
            }
            catch (Exception ex)
            {
                ;
            }
        }

        protected void CreateQPMfile_2_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelExport.OutputTxt((string)Session["filename2"], TextBox__MF.Text, HttpContext.Current.Response, Server);
                Session.Remove("filename2");
            }
            catch (Exception ex)
            {
                ;
            }
            finally
            {
                GridView_excel.DataSource = null;
                GridView_excel.DataBind();
            }
        }
    }
}